package com.fankui.serviceedu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankui.commonutils.JwtUtils;
import com.fankui.commonutils.R;
import com.fankui.serviceedu.client.OrderClient;
import com.fankui.serviceedu.entity.EduCourse;
import com.fankui.serviceedu.entity.frontvo.CourseDetailVo;
import com.fankui.serviceedu.entity.frontvo.CourseFrontVo;
import com.fankui.serviceedu.entity.vo.ChapterVo;
import com.fankui.serviceedu.service.EduChapterService;
import com.fankui.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName EduCourseFrontController
 * Description TODO
 * Date 2023/12/8 14:49
 *
 * @Author 范奎
 * @Since JDK 11
 */
@Api(description = "前台课程")
@RestController
@RequestMapping("/eduservice/coursefront")
public class EduCourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "条件查询课程分页数据")
    @PostMapping("getFrontCourseList/{current}/{size}")
    public R getFrontCourseList(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable long size,
                                @ApiParam(name = "courseFrontVo", value = "前台课程VO对象") @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(current, size);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "前台获取课程详情数据")
    @GetMapping("getFrontCourse/{courseId}")
    public R getFrontCourse(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId,
                            @ApiParam(name = "request", value = "request对象", required = true) HttpServletRequest request) {
        CourseDetailVo courseDetailVo = courseService.getFrontCourse(courseId);
        List<ChapterVo> chapterVoList = chapterService.getChapterListByCourseId(courseId);
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = false;
        if (!StringUtils.isEmpty(memberId)) {
            buyCourse = orderClient.isBuyCourse(courseId, memberId);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("courseDetailVo", courseDetailVo);
        map.put("chapterVoList", chapterVoList);
        map.put("isBuyCourse", buyCourse);
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据课程id获取课程详情")
    @GetMapping("getCourseDetails/{courseId}")
    public R getCourseDetails(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        CourseDetailVo courseDetailVo = courseService.getFrontCourse(courseId);
        return R.ok().data("courseDetailVo", courseDetailVo);
    }
}
